import { _decorator, Camera, CameraComponent, Component, EventKeyboard, EventTouch, geometry, Input, input, Node ,PhysicsSystem,SystemEvent} from 'cc';
import { Button_3D } from './Button_3D';
const { ccclass, property } = _decorator;

@ccclass('CameraEventHandler')
export class CameraEventHandler extends Component {

    public camera:Camera;

    protected onLoad(): void {
        this.camera = this.getComponent(CameraComponent);
        input.on(Input.EventType.MOUSE_DOWN,(event)=>{
            this.onTouchStart(event);
        });
    }

    onTouchStart(event){
        if(event.getButton() == 0){
            
            var ray = new geometry.Ray();
            this.camera.screenPointToRay(event.getLocationX(), event.getLocationY(), ray);
            
             if (PhysicsSystem.instance.raycast(ray)) {
                 const r = PhysicsSystem.instance.raycastResults;
                 for (let index = 0; index < r.length; index++) {
                    const element = r[index];
                     var btn =  element.collider.node.getComponent(Button_3D);
                     if(btn){
                        console.log("当前点击： " + element.collider.node.uuid);
                        btn.excute();
                     }
                }
            }
        }
     }
     
     
}


